﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using Jumbot.Common;
using JumbotCms.Common;
using System.Text;
namespace JumbotCms.WebFile.Admin
{
    public partial class _loginajax : JumbotCms.API.AdminCenter
    {
        private string _operType = string.Empty;
        private string _response = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            this._operType = q("oper");
            switch (this._operType)
            {
                case "leftmenu":
                    GetLeftMenu();
                    break;
                case "login":
                    Login();
                    break;
                case "logout":
                    Logout();
                    break;
                case "chkadminpower":
                    ChkAdminPower();
                    break;
                case "ajaxClearCache":
                    ajaxClearCache();
                    break;
                case "ajaxCreateSystemTree":
                    ajaxCreateSystemTree();
                    break;
                case "ajaxCreateSystemCount":
                    ajaxCreateSystemCount();
                    break;
                case "ajaxCreateIndexPage":
                    ajaxCreateIndexPage();
                    break;
                case "ajaxCreateRssMap":
                    ajaxCreateRssMap();
                    break;
                case "ajaxCreateSiteMap":
                    ajaxCreateSiteMap();
                    break;
                default:
                    DefaultResponse();
                    break;
            }
            Response.Write(this._response);
        }
        private void DefaultResponse()
        {
            Admin_Load("", "json");
            this._response = JsonResult(1, "成功登录");
        }
        private void GetLeftMenu()
        {
            Admin_Load("", "json");
            int menuId = Str2Int(q("m"), 1);
            int minId = 0;
            int maxId = 0;
            string[,] menu = leftMenu();
            StringBuilder sb = new StringBuilder();
            if (menuId < publicMenu)
            {
                minId = menuId;
                maxId = menuId;
            }
            else
            {
                minId = menuId;
                maxId = menu.GetLength(0) - 1;
            }
            int menuNum = (maxId - minId + 1);
            string firstlink = site.Dir + "admin/home.htm";
            bool searchlink = true;
            sb.Append("{result:\"1\", returnval:'获取成功', recordcount:" + (maxId - minId + 1) + ", table:[");
            int NO = 0;
            for (int i = minId; i < maxId + 1; i++)
            {
                if (menu[i, 0] == null) break;
                if (NO > 0) sb.Append(",");
                NO++;
                sb.Append("\n{no:" + NO + ", ");
                sb.Append("title:'" + menu[i, 0].Split('$')[0] + "', ");
                sb.Append("table:[");
                for (int j = 1; j < menu.GetLength(1); j++)
                {
                    if (menu[i, j] == null) break;
                    if (searchlink)
                    {
                        firstlink = menu[i, j].Split('|')[0];
                        searchlink = false;
                    }
                    if (j > 1) sb.Append(",");
                    sb.Append("{ischannel:'" + menu[i, 0].Split('$')[1] + "',");
                    sb.Append("url:'" + menu[i, j].Split('|')[0] + "',");
                    sb.Append("title:'" + menu[i, j].Split('|')[1] + "'");
                    if (menu[i, 0].Split('$')[1] == "1")
                        sb.Append(",channelid:'" + menu[i, j].Split('|')[2] + "'");
                    sb.Append("}");
                }
                sb.Append("]}");
            }
            sb.Append("],firstlink:'" + firstlink + "'}");
            this._response = sb.ToString();
        }
        private void Login()
        {
            string _name = f("name");
            string _pass = Jumbot.Common.Strings.Left(f("pass"), 14);
            string _code = f("code");
            if (Cookie.GetValue("ValidateCode") != _code)
            {
                this._response = "验证码错误";
                return;
            }
            int _type = Jumbot.Common.Validator.StrToInt(f("type"), 0);
            int iExpires = 0;
            if (_type > 0)
                iExpires = 60 * 60 * 24 * _type;//保存天数
            string _loginInfo = ChkAdminLogin(_name, _pass, iExpires);
            if (_loginInfo == "ok")
            {
                SaveAdminLog("登录后台");
            }
            this._response = _loginInfo;
        }
        private void Logout()
        {
            ChkAdminLogout();
            SaveAdminLog("退出后台");
            this._response = JsonResult(1, "成功退出");
        }
        private void ChkAdminPower()
        {
            Admin_Load(q("power"), "json");
            this._response = JsonResult(1, "身份合法");
        }
        private void ajaxClearCache()
        {
            Admin_Load("0000", "json");
            SetupSystemDate();
            new CreateJson().CreateSystemInfo(doh);
            this._response = JsonResult(1, "更新完成");
        }
        private void ajaxCreateSystemTree()
        {
            Admin_Load("0000", "json");
            CreateMenu("0");
            this._response = JsonResult(1, "树更新完成");
        }
        private void ajaxCreateSystemCount()
        {
            Admin_Load("0000", "json");
            CreateCount("0");
            this._response = JsonResult(1, "统计更新完成");
        }
        private void ajaxCreateIndexPage()
        {
            Admin_Load("", "json");
            CreateDefaultFile();
            this._response = JsonResult(1, "网站首页更新完成");
        }
        private void ajaxCreateRssMap()
        {
            Admin_Load("", "json");
            string PageStr = Jumbot.Common.DirFile.ReadFile(site.Dir + "templates/system_rssmap.htm");
            JumbotCms.API.TemplateEngine te = new JumbotCms.API.TemplateEngine(doh, "0");
            te.IsHtml = site.IsHtml;
            te.PageNav = "<a href=\"/\">首页</a>&nbsp;&gt;&nbspRSS订阅";
            te.PageTitle = "RSS订阅 - " + site.Name + site.TitleTail;
            te.PageKeywords = site.Keywords;
            te.PageDescription = "RSS订阅";
            te.ReplacePublicTag(ref PageStr, "0", te.DefaultSkinId);
            te.ReplaceRepeatTag(ref PageStr);
            string TxtFile = Server.MapPath(site.Dir) + "rssmap" + site.StaticExt;
            System.IO.StreamWriter sw = new System.IO.StreamWriter(TxtFile, false, System.Text.Encoding.UTF8);
            sw.Write(PageStr);
            sw.Close();
            this._response = JsonResult(1, "站内导航更新完成");
        }
        private void ajaxCreateSiteMap()
        {
            Admin_Load("", "json");
            string PageStr = Jumbot.Common.DirFile.ReadFile(site.Dir + "templates/system_sitemap.htm");
            JumbotCms.API.TemplateEngine te = new JumbotCms.API.TemplateEngine(doh, "0");
            te.IsHtml = site.IsHtml;
            te.PageNav = "<a href=\"/\">首页</a>&nbsp;&gt;&nbsp站内导航";
            te.PageTitle = "站内导航 - " + site.Name + site.TitleTail;
            te.PageKeywords = site.Keywords;
            te.PageDescription = "站内导航";
            te.ReplacePublicTag(ref PageStr, "0", te.DefaultSkinId);
            te.ReplaceRepeatTag(ref PageStr);
            Jumbot.Common.DirFile.SaveFile(PageStr, "~/sitemap" + site.StaticExt);
            this._response = JsonResult(1, "站内导航更新完成");
        }
    }
}
